//
//  CleanView.swift
//  AirlineReservation
//
//  Created by Kevin on 2020/3/9.
//  Copyright © 2020 Kevin. All rights reserved.
//

import UIKit

public protocol CleanViewDelegate {
    func clear_remove()
    func clear_finish()
}

class CleanView: UIView {
    
    // 白色背景
    private lazy var whiteView: OvalView = {
        let uiY = frame.height - 191
        let ui = OvalView(frame: CGRect(x: 0, y: uiY, width: frame.width, height: 191))
        ui.backgroundColor = UIColor.white
        return ui
    }()
    
    // 代理
    public var delegate:CleanViewDelegate?
    
    override func layoutSubviews() {
        // 背景色
        backgroundColor = UIColor.init(displayP3Red: 0, green: 0, blue: 0, alpha: 0.4)
        // 白色背景
        addSubview(whiteView)
        // 删除描述
        let deleteDesc = UILabel(title: "删除应用内的所有数据后,需要重新登录", color: UIColor.gray, size: 13, align:.center)
        deleteDesc.frame = CGRect(x: 0, y: 0, width: frame.width, height: MARGIN70)
        whiteView.addSubview(deleteDesc)
        // 下划线
        let line = UIView(frame: CGRect(x: 0, y: deleteDesc.frame.maxY, width: frame.width, height: 1))
        line.backgroundColor = COLOR_LINE
        whiteView.addSubview(line)
        // 删除文本
        let deleteBtn = UILabel(title: "清除缓存", color: UIColor.red, size: 15, align:.center)
        deleteBtn.frame = CGRect(x: 0, y: line.frame.maxY, width: frame.width, height: MARGIN55)
        whiteView.addSubview(deleteBtn)
        deleteBtn.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(clickClearBtn)))
        // 横线粗
        let line2 = UIView(frame: CGRect(x: 0, y: deleteBtn.frame.maxY, width: frame.width, height: 10))
        line2.backgroundColor = COLOR_LINE2
        whiteView.addSubview(line2)
        // 取消按钮
        let cancelBtn = UILabel(title: "取消", color: COLOR_NICE, size: 15, align:.center)
        cancelBtn.frame = CGRect(x: 0, y: line2.frame.maxY, width: frame.width, height: MARGIN55)
        whiteView.addSubview(cancelBtn)
        cancelBtn.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(clickClearCancel)))
    }
    
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        guard let point = touches.first?.location(in: self) else {
            return
        }
        if !whiteView.frame.contains(point) {
            delegate?.clear_remove()
        }
    }
    
    @objc func clickClearBtn() -> Void {
        delegate?.clear_finish()
    }
    
    @objc func clickClearCancel() -> Void {
        delegate?.clear_remove()
    }
}
